package com.leoman.spinningmachine.tab1;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.leoman.helper.adapter.BaseRecyclerAdapter;
import com.leoman.helper.util.RefreshUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.bean.NewsListBean;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.BGARefreshUtil;
import com.leoman.spinningmachine.util.DialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * Created by Administrator on 2018-05-11.
 */
public class TzggActivity extends BaseActivity{

    @BindView(R.id.bgaRefreshLayout)
    BGARefreshLayout bgaRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private TzggAdapter adapter;
    private List<NewsListBean> noticeLists = new ArrayList<>();
    private int page;
    private boolean isLastPage;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_tzgg;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initTitle("通知公告");
        adapter = new TzggAdapter(this,R.layout.item_tzgg, null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        adapter.setClickListener(new BaseRecyclerAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                gotoFlagOtherActivity(Detail22Activity.class,noticeLists.get(position).getId());
            }
        });
    }

    @Override
    protected void initData() {

        page = 1;
        bgaRefreshLayout.setDelegate(this);
        RefreshUtil.getInstance().initRefresh(this,bgaRefreshLayout);
        bgaRefreshLayout.beginRefreshing();
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        super.OnSuccessData(apiResponse, tag);
        BGARefreshUtil.endLoading(tag,bgaRefreshLayout);
        isLastPage = BGARefreshUtil.getIsLastPage(apiResponse);
        if(page == 1){
            noticeLists.clear();
        }
        noticeLists.addAll(apiResponse.data.list);
        adapter.setData(noticeLists);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        page = 1;
        WebServiceApi.getInstance().newsQueryList("1",""+page, ""+Constant.pageSize,this,1);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if(isLastPage){
            return false;
        }
        page++;
        DialogUtils.getInstance().show(this);
        WebServiceApi.getInstance().newsQueryList("1",""+page, ""+Constant.pageSize,this,2);
        return true;
    }

}
